import 'package:get/get.dart';
import 'package:flutter/material.dart';

class GetxMixinDemoController extends GetxController
    with WidgetsBindingObserver {
  var lifecycle = 'AppLifecycleState: unknown'.obs;

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void onClose() {
    WidgetsBinding.instance.removeObserver(this);
    super.onClose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    lifecycle.value = 'AppLifecycleState: $state';
  }

  String get lifecycleDesc {
    switch (lifecycle.value) {
      case 'AppLifecycleState: AppLifecycleState.resumed':
        return '应用在前台';
      case 'AppLifecycleState: AppLifecycleState.paused':
        return '应用在后台';
      case 'AppLifecycleState: AppLifecycleState.inactive':
        return '应用非活动';
      case 'AppLifecycleState: AppLifecycleState.detached':
        return '应用已分离';
      default:
        return lifecycle.value;
    }
  }
}
 